var db = require('../conf/db')


const getDicts = async(req, res) => {
    let sql = 'SELECT id,dict_type,remark,dict_key,dict_value FROM `mdm`.`sys_dict` '
    let data = req.body
    if (data.dict_type || data.remark) {
        sql += 'WHERE 1=1'
        if (data.dict_type) {
            sql += ' and dict_type like "%' + data.dict_type + '%"'
        }
        if (data.remark) {
            sql += ' and remark like "%' + data.remark + '%"'
        }
    }
    sql += 'ORDER  BY dict_type '
    db.query(sql, [], function(results, fields) {
        // 以json的形式返回
        res.send({ code: 20000, data: results })
    })
}
const insertDicts = async(req, res) => {
    let data = req.body
    let sql = 'INSERT INTO `mdm`.`sys_dict`(`dict_type`, `dict_key`, `dict_value`, `remark`) VALUES ("' + (data.dict_type ? data.dict_type : '') + '", "' + (data.dict_key ? data.dict_key : '') + '", "' + (data.dict_value ? data.dict_value : '') + '",  "' + (data.remark ? data.remark : '') + '") '
    db.query(sql, [], function(results, fields) {
        // 以json的形式返回
        res.send({ code: 20000, data: results })
    })
}
const delDicts = async(req, res) => {
    let sql = 'DELETE FROM `mdm`.`sys_dict` '
    let data = req.body
    sql += 'WHERE `id` =' + data.id
    db.query(sql, [], function(results, fields) {
        // 以json的形式返回
        res.send({ code: 20000, data: results })
    })
}
const editDicts = async(req, res) => {
    let sql = 'UPDATE `mdm`.`sys_dict` SET '
    let data = req.body
    for (const key in data) {
        if (key != 'id' && key != 'iseditor') {
            sql += key + '= "' + data[key] + '",'

        }
    }
    sql = sql.substring(0, sql.length - 1)
    sql += 'WHERE `id` =' + data.id
    db.query(sql, [], function(results, fields) {
        // 以json的形式返回
        res.send({ code: 20000, data: results })


    })
}
module.exports = {
    getDicts,
    editDicts,
    delDicts,
    insertDicts
}